package com.woniuxy.enjoylife.service.impl;

import com.woniuxy.enjoylife.service.FileService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ResourceBundle;
import java.util.UUID;

@Service
public class FileServiceImpl implements FileService {


    @Override
    public String uploadImage(MultipartFile file) {
        String name = UUID.randomUUID().toString() + ".jpg";
        ResourceBundle bundle = ResourceBundle.getBundle("imagePath");
        String returnName = bundle.getString("imagePath") + name;

//        String path = Thread.currentThread().getContextClassLoader().getResource("").getPath() + "file" + File.separator + "images" + File.separator;
//        String fileName = path + name;
//        File fileDir = new File(path);
//        File images = new File(fileName);

        //临时方案（上线后更改）
        File images=getImages();
        File newFile=new File(images,name);

        try {
            if (!images.exists()) images.mkdirs();
            file.transferTo(newFile);
//            return fileName;
            return returnName;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void getImage(String name, HttpServletResponse response) {
//        String path = Thread.currentThread().getContextClassLoader().getResource("").getPath() + "file" + File.separator + "images" + File.separator;
//        File file = new File(path + name);

        //临时方案
        File file = new File(getImages(),name);


        response.setDateHeader("Expires", -1);
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("imge/jpeg");
        BufferedImage image = null;
        try {
            image = ImageIO.read(file);
        } catch (IOException e) {
//            e.printStackTrace();
            System.out.println("获取图片失败,请在推送本地分支时上传图片");
        }
        try {
            ImageIO.write(image, "png", response.getOutputStream());
        } catch (IOException e) {
//            e.printStackTrace();
            System.out.println("获取图片失败,请在推送本地分支时上传图片");
        }catch (IllegalArgumentException e){
            System.out.println("获取图片失败,请在推送本地分支时上传图片");
        }
    }

    private File getImages() {
//        String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
//        File enjoy=new File(rootPath).getParentFile().getParentFile();
//        File src=new File(enjoy,"src");
//        File main=new File(src,"main");
//        File resources=new File(main,"resources");
//        File images=new File(resources,"images");
//        File images=new File(rootPath,"images");

//        String staticPath = this.getClass().getClassLoader().getResource("static").getFile();
//        File images=new File(staticPath,"images");
        ResourceBundle bundle = ResourceBundle.getBundle("imagePath");
        File images=new File(bundle.getString("filePath"));
        return images;
    }
}
